<?php
class usedetailAction extends backendAction {

	public function index(){
		$p = ($_GET['p'])?$_GET['p']:0;
		session('callback_url','http://'.$_SERVER['HTTP_HOST'].'/index.php?g=admin&m=usedetail&a=index&menuid=328&p='.$p);
    	$Use = M('use_detail');
    	$list = $Use->order('id desc')->page($p.',25')->select();
    	$this->assign('list',$list);
    	import("ORG.Util.Page"); 
    	$count = $Use->count();
    	$Page = new Page($count,25); 
		$show = $Page->show();
		$this->assign('page',$show);
		$this->display();
	}

	public function add(){
		$this->display(); // 输出模板
	}

	public function save(){
		$data['use_time'] = trim($_POST['nowdate']);
        $data['update_place'] = trim($_POST['update_place']);
    	$data['note'] = trim($_POST['note']);

    	if (!$data['use_time']) {
    		$this->error('请输入使用日期');
    	}

    	if (!$data['update_place']) {
    		$this->error('请输入更换位置');
    	}
    	if (!$data['note']) {
    		$this->error('请输入备注');
    	}

    	$Use = M('use_detail');
        $res = $Use->data($data)->add();
		if ($res) {
			$this->success(L('operation_success'), U('index'));
		}else{
			$this->error(L('operation_failure'));
		}
	}

	public function del(){
		$Use = M("use_detail"); // 实例化 User 对象
		$res = $Use->where('id='.(int)$_GET['id'] )->delete(); 
		header('Location:'.session('callback_url'));
	}

	public function edit(){
		$Use = M("use_detail");
    	$id = $_GET['id'];
    	$item = $Use->where(array('id'=>$id))->find();
    	$this->assign('item',$item); // 赋值分页输出
		$this->display(); // 输出模板
	}

	 public function update(){
	 	$id=$_POST['id'];
    	$data['use_time'] = trim($_POST['nowdate']);
        $data['update_place'] = trim($_POST['update_place']);
    	$data['note'] = trim($_POST['note']);

    	$ref = M("use_detail")->where(array('id'=>$id))->save($data);
    	
		if($ref)
			$this->success(L('operation_success'), U('index'));
		else
			$this->error(L('operation_failure'));
	 }

}